package initAll;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import com.aventstack.extentreports.ExtentTest;

import javaUnit.ThreadTime;
import seleniumUnit.linkOperate;

public class login2 {

	private static final Logger log = LogManager.getLogger(login2.class.getName());
	private static String url = "http://10.168.1.250:9080/";
	static EventFiringWebDriver driver;
	ExtentTest test;
	linkOperate Operate;
	//public static WebElement login_title,edit_UserName,edit_Password,btn_login,btn_language_English,btn_language_chinese,btn_userName,btn_exit_login,table_error_UserName,table_error_password,msg_sucess,btn_determine;
			
	public login2(ExtentTest test, EventFiringWebDriver driver) {
		this.driver = driver;
		this.test = test;
		//PageFactory.initElements(driver, this);
//		Operate = new linkOperate(test, driver);
//		login_title=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']"));
//		edit_UserName=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']"));
//		edit_Password=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='password']"));
//		btn_login=driver.findElement(By.xpath("//button[@class='el-button form-submit el-button--default el-button--large']/span"));
//		btn_language_English=driver.findElement(By.xpath("//span[text()='English']"));
//		btn_language_chinese=driver.findElement(By.xpath("//span[text()='中文']"));
//		btn_userName=driver.findElement(By.xpath("//div[2]/div/div/div/span[1]"));
//		btn_exit_login=driver.findElement(By.xpath("//a[@href='javascript:' and contains(@class,'dropdown-btn')]/i[@class='icon fa fa-sign-out']"));
//		table_error_UserName=driver.findElement(By.xpath("//form/div[1]/div/div[2]"));
//		table_error_password=driver.findElement(By.xpath("//form/div[2]/div/div[2]"));
//		msg_sucess=driver.findElement(By.xpath("//div[@class='el-message__group']"));
//		btn_determine=driver.findElement(By.xpath("//div[@class='el-message-box__btns']/button[2]"));
	}

	//form login title  GetText(ch/en)=协同教学管理平台/Ximmerse Operation Platform
	@FindBy(how = How.XPATH, xpath = "//div[@class='login-title']/div")
	public WebElement login_title=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']"));
	
	//账户名编辑框  GetText(ch/en)=请输入账户名/Please enter account name
	@FindBy(how = How.XPATH, xpath = "//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']")
	public WebElement edit_UserName=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']"));

	//密码编辑框  GetText(ch/en)=请输入密码/Please enter account password
	@FindBy(how = How.XPATH, xpath = "//div[@class='el-form-item__content'][1]/div[1]/input[@type='password']")
	public WebElement edit_Password=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='password']"));

	//登录按钮  GetText(ch/en)=登录/Login
	@FindBy(how = How.XPATH, xpath = "//button[@class='el-button form-submit el-button--default el-button--large']/span")
	public WebElement btn_login=driver.findElement(By.xpath("//button[@class='el-button form-submit el-button--default el-button--large']/span"));

	//选择英文按钮
	@FindBy(how = How.XPATH, xpath = "//span[text()='English']")
	public WebElement btn_language_English=driver.findElement(By.xpath("//span[text()='English']"));

	//选择中文按钮
	@FindBy(how = How.XPATH, xpath = "//span[text()='中文']")
	public WebElement btn_language_chinese=driver.findElement(By.xpath("//span[text()='中文']"));
	
	//登陆成功后的用户名
	@FindBy(how = How.XPATH, xpath = "//div[2]/div/div/div/span[1]")
	public WebElement btn_userName=driver.findElement(By.xpath("//div[2]/div/div/div/span[1]"));

	//退出登录按钮    GetText(ch/en)=退出登录/Logout
//	@FindBy(how = How.XPATH, xpath = "//a[@href='javascript:' and contains(@class,'dropdown-btn')]/i[@class='icon fa fa-sign-out']")
//	WebElement btn_exit_login;

	//账户名为空提示   GetText(ch/en)=请输入账户名/Please enter account name
//	@FindBy(how = How.XPATH, xpath = "//form/div[1]/div/div[2]")
//	WebElement table_error_UserName;

	//账户名为空提示   GetText(ch/en)=请输入账户密码/Please enter account password
//	@FindBy(how = How.XPATH, xpath = "//form/div[2]/div/div[2]")
//	WebElement table_error_password;

	//消息成功  GetText(ch/en)=sucess
//	@FindBy(how = How.XPATH, xpath = "//div[@class='el-message__group']")
//	WebElement msg_sucess;
	
	//确认按钮  GetText(ch/en)=确定/OK
//	@FindBy(how = How.XPATH, xpath = "//div[@class='el-message-box__btns']/button[2]")
//	WebElement btn_determine;
	
	
	
	//输入用户名和密码
	public void inputUserInfo(String userName,String passWord) {
		edit_UserName.click();
		edit_UserName.sendKeys(userName);
		edit_Password.click();
		edit_Password.sendKeys(passWord);
	}
	
	//点击登陆按钮
	public void clickBtnLogin(){
		btn_login.click();
	}
	
	//退出登录
	public void exitUsername(int wait){
		//等待页加载
		WebElement userName=Operate.getClickWebElement(driver,"//div[2]/div/div/div/span[1]","xpath",wait);
		ThreadTime.sleepTime(1);
		userName.click();
		
		WebElement ele=Operate.getClickWebElement(driver,"//a[@href='javascript:' and contains(@class,'dropdown-btn')]/i[@class='icon fa fa-sign-out']","xpath",wait);
		ThreadTime.sleepTime(1);
		ele.click();
		
		WebElement btn_determine=Operate.getClickWebElement(driver,"//div[@class='el-message-box__btns']/button[2]","xpath",wait);
		ThreadTime.sleepTime(1);
		btn_determine.click();
		
	}
	
	//界面选择英文
	public boolean choiceEnglish() {
		btn_language_English.click();
		return true;
	}
	
	//界面选择中文
	public void btn_language_chinese() {
		btn_language_chinese.click();
	}

	//等待number S检查是否弹出sucess
	public boolean isMsgSucess(int number){
		return Operate.getWaitIsDisplay(driver,"//div[@class='el-message__group']","xpath",number);
	}
	
	//返回用户名
	public String getUserName(){
		return btn_userName.getText();
	}
	
	
	public void getUrl(String ieUrl) {
		String CurrentUrl = driver.getCurrentUrl();
		if (!driver.getCurrentUrl().equals(ieUrl)) {
			log.error("URL Error!" + "CurrentUrl=" + CurrentUrl + "   targetURL=" + ieUrl);
			driver.get(ieUrl);
		}
	}
}
